/*
 * Copyright (C) 2011 flashflexpro@gmail.com  All Rights Reserved.  No
 *   use, copying or distribution of this work may be made except in
 *   accordance with a valid license agreement from flashflexpro@gmail.com
 *   This notice must be included on all copies, modifications and
 *   derivatives of this work.
 *
 *   flashflexpro@gmail.com MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT
 *   THE SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED,
 *   INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 *   NON-INFRINGEMENT. flashflexpro@gmail.com SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED
 *   BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS
 *   SOFTWARE OR ITS DERIVATIVES.
 */

package com.flashflexpro.as3lib {
import mx.collections.ArrayCollection;
import mx.collections.IList;
import mx.core.IVisualElementContainer;

import org.flexunit.asserts.assertEquals;

public class TestListToVisualElementContainerSyncer {


    public function TestListToVisualElementContainerSyncer() {
    }

    private var modelList:IList;
    private var viewList:IVisualElementContainer;

    [Test]
    public function testSync():void {

        modelList = new ModelList([ new ModelEle(0), new ModelEle(1), new ModelEle(2), new ModelEle(3), new ModelEle(4)]);
        viewList = new ViewList();

        var syncer:ListToVisualElementContainerSyncer = new ListToVisualElementContainerSyncer(null, modelList, viewList);

        //test initialize
        syncer.syncListToVContainer(doesViewEleMatchModelEle, newViewEleFromModelEle);
        assertEquals("", 5,  viewList.numElements);
        assertEquals("", 0,  (viewList.getElementAt(0) as ViewEle).model.index);
        assertEquals("", 1,  (viewList.getElementAt(1) as ViewEle).model.index);
        assertEquals("", 2,  (viewList.getElementAt(2) as ViewEle).model.index);
        assertEquals("", 3,  (viewList.getElementAt(3) as ViewEle).model.index);
        assertEquals("", 4,  (viewList.getElementAt(4) as ViewEle).model.index);

        //test replace
        modelList.setItemAt( new ModelEle(6), 0);
        
        syncer.syncListToVContainer(doesViewEleMatchModelEle, newViewEleFromModelEle);
        assertEquals("", 5,  viewList.numElements);
        assertEquals("", 6,  (viewList.getElementAt(0) as ViewEle).model.index);
        assertEquals("", 1,  (viewList.getElementAt(1) as ViewEle).model.index);
        assertEquals("", 2,  (viewList.getElementAt(2) as ViewEle).model.index);
        assertEquals("", 3,  (viewList.getElementAt(3) as ViewEle).model.index);
        assertEquals("", 4,  (viewList.getElementAt(4) as ViewEle).model.index);

        //test add
        modelList.addItem( new ModelEle(7));

        syncer.syncListToVContainer(doesViewEleMatchModelEle, newViewEleFromModelEle);
        assertEquals("", 6,  viewList.numElements);
        assertEquals("", 6,  (viewList.getElementAt(0) as ViewEle).model.index);
        assertEquals("", 1,  (viewList.getElementAt(1) as ViewEle).model.index);
        assertEquals("", 2,  (viewList.getElementAt(2) as ViewEle).model.index);
        assertEquals("", 3,  (viewList.getElementAt(3) as ViewEle).model.index);
        assertEquals("", 4,  (viewList.getElementAt(4) as ViewEle).model.index);
        assertEquals("", 7,  (viewList.getElementAt(5) as ViewEle).model.index);

        //test remove
        modelList.removeItemAt( 2 );
        syncer.syncListToVContainer(doesViewEleMatchModelEle, newViewEleFromModelEle);
        assertEquals("", 5,  viewList.numElements);
        assertEquals("", 6,  (viewList.getElementAt(0) as ViewEle).model.index);
        assertEquals("", 1,  (viewList.getElementAt(1) as ViewEle).model.index);
        assertEquals("", 3,  (viewList.getElementAt(2) as ViewEle).model.index);
        assertEquals("", 4,  (viewList.getElementAt(3) as ViewEle).model.index);
        assertEquals("", 7,  (viewList.getElementAt(4) as ViewEle).model.index);

        modelList.addItemAt( modelList.removeItemAt( 3 ), 1 );
        syncer.syncListToVContainer(doesViewEleMatchModelEle, newViewEleFromModelEle);
        assertEquals("", 5,  viewList.numElements);
        assertEquals("", 6,  (viewList.getElementAt(0) as ViewEle).model.index);
        assertEquals("", 4,  (viewList.getElementAt(1) as ViewEle).model.index);
        assertEquals("", 1,  (viewList.getElementAt(2) as ViewEle).model.index);
        assertEquals("", 3,  (viewList.getElementAt(3) as ViewEle).model.index);
        assertEquals("", 7,  (viewList.getElementAt(4) as ViewEle).model.index);



    }

    private function newViewEleFromModelEle(modelEle:ModelEle):ViewEle {
        return new ViewEle(modelEle);
    }

    private function doesViewEleMatchModelEle(viewEle:ViewEle, modelEle:ModelEle):Boolean {
        return viewEle.model == modelEle;
    }
}
}
